package com.cyt.website.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.website.dao.WebsiteApplicationCategoryDao;
import com.cyt.website.model.po.WebsiteApplicationCategory;
import com.cyt.website.model.vo.WebsiteApplicationCategoryVo;
import com.cyt.website.service.WebsiteApplicationCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 官网案例分类关联-官网案例分类关联Service层实现类
 *
 * @author congcong tao
 * @date 2019-8-13 9:50:01
 */
@Service("websiteApplicationCategoryService")
public class WebsiteApplicationCategoryServiceImpl extends BaseServiceImpl<WebsiteApplicationCategory> implements WebsiteApplicationCategoryService {

    @Resource
    private WebsiteApplicationCategoryDao websiteApplicationCategoryDao;

    @Autowired
    public void setBaseDao(WebsiteApplicationCategoryDao websiteApplicationCategoryDao) {
        this.baseDao = websiteApplicationCategoryDao;
    }

    /**
     * 分页查询
     *
     * @param websiteApplicationCategoryVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:50:01
     */
    @Override
    public ReturnResult findPage(WebsiteApplicationCategoryVo websiteApplicationCategoryVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        Example example = new Example(WebsiteApplicationCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        List<WebsiteApplicationCategory> list = websiteApplicationCategoryDao.selectByExample(example);
        PageInfo<WebsiteApplicationCategory> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 分页查询
     *
     * @param websiteApplicationCategoryVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:50:01
     */
    @Override
    public ReturnResult findVoPage(WebsiteApplicationCategoryVo websiteApplicationCategoryVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WebsiteApplicationCategoryVo> list = websiteApplicationCategoryDao.selectVoList(websiteApplicationCategoryVo);
        PageInfo<WebsiteApplicationCategoryVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * list查询
     *
     * @param websiteApplicationCategoryVo 接受参数对象
     * @return List<WebsiteApplicationCategory>
     * @author congcong tao
     * @date 2019-8-13 9:50:01
     */
    @Override
    public List<WebsiteApplicationCategory> findList(WebsiteApplicationCategoryVo websiteApplicationCategoryVo) {
        Example example = new Example(WebsiteApplicationCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        if (StringUtils.isNotBlank(websiteApplicationCategoryVo.getApplicationId())) {
            criteria.andEqualTo("applicationId", websiteApplicationCategoryVo.getApplicationId());
        }
        return websiteApplicationCategoryDao.selectByExample(example);
    }

    /**
     * list查询
     *
     * @param websiteApplicationCategoryVo 接受参数对象
     * @return List<WebsiteApplicationCategory>
     * @author congcong tao
     * @date 2019-8-13 9:50:01
     */
    @Override
    public List<WebsiteApplicationCategoryVo> findVoList(WebsiteApplicationCategoryVo websiteApplicationCategoryVo) {
        return websiteApplicationCategoryDao.selectVoList(websiteApplicationCategoryVo);
    }

    /**
     * 单个对象查询
     *
     * @param websiteApplicationCategoryVo 接受参数对象
     * @return WebsiteApplicationCategory
     * @author congcong tao
     * @date 2019-8-13 9:50:01
     */
    @Override
    public WebsiteApplicationCategory findOne(WebsiteApplicationCategoryVo websiteApplicationCategoryVo) {
        WebsiteApplicationCategory websiteApplicationCategory = new WebsiteApplicationCategory();
        if (StrUtil.isEmpty(websiteApplicationCategoryVo.getId())) {
            websiteApplicationCategory.setId(websiteApplicationCategoryVo.getId());
        }
        return websiteApplicationCategoryDao.selectOne(websiteApplicationCategory);
    }

    /**
     * 单个对象查询
     *
     * @param websiteApplicationCategoryVo 接受参数对象
     * @return WebsiteApplicationCategory
     * @author congcong tao
     * @date 2019-8-13 9:50:01
     */
    @Override
    public WebsiteApplicationCategoryVo findVoOne(WebsiteApplicationCategoryVo websiteApplicationCategoryVo) {
        return websiteApplicationCategoryDao.selectVoOne(websiteApplicationCategoryVo);
    }

    /**
     * 添加或更新
     *
     * @param websiteApplicationCategoryVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:50:01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WebsiteApplicationCategoryVo websiteApplicationCategoryVo, SysUser sysUser) {
        WebsiteApplicationCategory websiteApplicationCategory;
        //初始化对象
        if (StrUtil.isEmpty(websiteApplicationCategoryVo.getId())) {
            websiteApplicationCategory = new WebsiteApplicationCategory(sysUser);
        } else {
            websiteApplicationCategory = websiteApplicationCategoryDao.selectByPrimaryKey(websiteApplicationCategoryVo.getId());
        }
        //设置属性值
        websiteApplicationCategory.setApplicationId(websiteApplicationCategoryVo.getApplicationId());
        websiteApplicationCategory.setCategoryId(websiteApplicationCategoryVo.getCategoryId());
        //提交数据库
        if (StrUtil.isEmpty(websiteApplicationCategoryVo.getId())) {
            websiteApplicationCategoryDao.insert(websiteApplicationCategory);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            websiteApplicationCategoryDao.updateByPrimaryKey(websiteApplicationCategory);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param websiteApplicationCategoryVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:50:01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WebsiteApplicationCategoryVo websiteApplicationCategoryVo, SysUser sysUser) {
        //修改字段
        WebsiteApplicationCategory websiteApplicationCategory = new WebsiteApplicationCategory();
        websiteApplicationCategory.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        //筛选条件
        Example example = new Example(WebsiteApplicationCategory.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteApplicationCategoryVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteApplicationCategoryVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        websiteApplicationCategoryDao.updateByExampleSelective(websiteApplicationCategory, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param websiteApplicationCategoryVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:50:01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WebsiteApplicationCategoryVo websiteApplicationCategoryVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WebsiteApplicationCategory.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteApplicationCategoryVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteApplicationCategoryVo.getId().split(",")));
        }
        if (StringUtils.isNotBlank(websiteApplicationCategoryVo.getApplicationId())) {
            criteria.andEqualTo("applicationId", websiteApplicationCategoryVo.getApplicationId());
        }
        websiteApplicationCategoryDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public ReturnResult batchSave(String applicationId, String categoryIds, SysUser sysUser) {
        WebsiteApplicationCategoryVo websiteApplicationCategoryVo = new WebsiteApplicationCategoryVo();
        websiteApplicationCategoryVo.setApplicationId(applicationId);
        delete(websiteApplicationCategoryVo, sysUser);
        if (StringUtils.isEmpty(categoryIds)) {
            new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
        }
        String[] categoryIdArr = categoryIds.split(",");
        for (String categoryId : categoryIdArr) {
            WebsiteApplicationCategory websiteApplicationCategory = new WebsiteApplicationCategory();
            websiteApplicationCategory.setId(BaseUtil.uId());
            websiteApplicationCategory.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
            websiteApplicationCategory.setApplicationId(applicationId);
            websiteApplicationCategory.setCategoryId(categoryId);
            this.insert(websiteApplicationCategory);
        }
        return new ReturnResult(ErrorCode.CODE_0, ErrorCode.CODE_0_MSG);
    }
}